//
//  lunbo.m
//  轮播图封装
//
//  Created by lmt on 16/1/12.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "lunbo.h"
#import "SKErJiVc.h"
#import <AFNetworking.h>
#import "ShiKeModel.h"

@interface lunbo ()
@property(nonatomic,retain)NSMutableArray *lunboArr;
@property(nonatomic,retain)NSMutableArray *arrr;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation lunbo 

#pragma mark - 图片数组轮播图
- (instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self addSubview:_scrollView];
        self.i = 0;
        [self lunbotuWithRect:frame array:arr];
    }
    return self;
}
- (void)lunbotuWithRect:(CGRect)rect array:(NSMutableArray *)arr
{
    
    _scrollView.frame = rect;
    [arr addObject:arr.firstObject];
    // 设置大小
    _scrollView.contentSize = CGSizeMake(self.frame.size.width * arr.count, 0);
    // 设置代理
    _scrollView.delegate = self;
    // 整页滚
    _scrollView.pagingEnabled = YES;
    
    
    
    // 对scroll添加图片
    for (NSInteger i = 0; i < arr.count; i++) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        imgView.image = arr[i];
        [_scrollView addSubview:imgView];
    }
    
    // 创建pageControl
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 50, 200, 40)];
    self.page.center = CGPointMake(self.center.x, self.page.center.y);
    self.page.numberOfPages = arr.count - 1;
   // self.page.backgroundColor = [UIColor blackColor];
    [self addSubview:self.page];
    
    // 记录数组个数
    self.arrCount = arr.count;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    
    
}

#pragma mark - 字符串数组轮播图
- (instancetype)initWithFrame:(CGRect)frame stringArray:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc] init];
        [self addSubview:_scrollView];
        self.i = 0;
        
        _scrollView.frame = frame;
        // 设置大小
        _scrollView.contentSize = CGSizeMake(self.frame.size.width * arr.count, 0);
        // 设置代理
        _scrollView.delegate = self;
        // 整页滚
        _scrollView.pagingEnabled = YES;
        
        [arr addObject:arr.firstObject];
        
        // 轮播图添加图片
        for (NSInteger i = 0; i < arr.count; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
            [imgView sd_setImageWithURL:[NSURL URLWithString:arr[i]]];
            [_scrollView addSubview:imgView];
        }
        // 创建pageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 50, 200, 40)];
        self.page.center = CGPointMake(self.center.x, self.page.center.y);
        self.page.numberOfPages = arr.count - 1;
        // self.page.backgroundColor = [UIColor blackColor];
        [self addSubview:self.page];
        
        [self shuju];
        
        // 记录数组个数
        self.arrCount = arr.count;
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
        // 轻拍按钮
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//        [self.scrollView addGestureRecognizer:tap];
//        [tap release];
    }
    return self;
}

#pragma mark - 点击方法
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSInteger j = self.page.currentPage;
    
    
    
    if (j < _arrCount && j > 0) {
        j = j;
    } else {
        j = 0;
    }
    
    NSString *str = _arrr[j];
    [self.delegate passValue:str];
    
}

-(void)shuju
{
    NSString *str = @"http://api.izhangchu.com/?size=20&page=1&methodName=CourseIndex&version=4.3&";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:str parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr1 = responseObject[@"data"][@"top"];
        self.lunboArr = [ShiKeModel modelHanderWithArray:arr1];
        self.arrr = [NSMutableArray array];
        for (ShiKeModel *m in _lunboArr) {
            NSString *str = m.banner_link;
            [_arrr addObject:str];
        }
       
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}





#pragma mark - 自动播放装置 左向右滚
- (void)zidong
{
    // 因为有8张图 所以i值为0~7 八个数
    if (_i < _arrCount) {
        // 让scroll 自己变化 带动画的变
        [self.scrollView setContentOffset:CGPointMake(_scrollView.frame.size.width * _i , 0) animated:YES];
        // 判断是第几个图 是最后图的时候
        if (_i == _arrCount - 1) {
            // 最后一个的pageControl跳到第一个 因为有8个图片 但是只有7个点
            _page.currentPage = 0;
        } else {
            // 其他的时候 page的点 也自己随着i变化
            _page.currentPage = _i;
        }
        _i++;
        
    } else {
        // 超过了 就让i变回 从头开始
        _i = 0;
        _page.currentPage = _i;
        // 最后一张假图变回第一张图 不用动画
        [_scrollView setContentOffset:CGPointMake(0 , 0) animated:NO];
        
    }
    
    
}

// 滚动开始
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 停止自动滚动
    [_timer invalidate];
    _timer = nil;
   
    
}

// 滚动减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 匹配翻页页码
    
    if (_scrollView.contentOffset.x == _scrollView.frame.size.width * (_arrCount - 1)) {
        _scrollView.contentOffset = CGPointZero;
        _page.currentPage = 0;
    } else {
        _page.currentPage = _scrollView.contentOffset.x / _scrollView.frame.size.width;
    }
    
     _i = self.page.currentPage;
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(zidong) userInfo:nil repeats:YES];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
